package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;



	//Request URL: http://localhost:8500/api/account/userAccount/getAvailableAmount
	@GetMapping("/getAvailableAmount")
	@Operation(summary = "查询当前用户账户余额")
	@TingshuLogin
	public Result getAvailableAmount(){
		BigDecimal AvailableAmount = userAccountService.getAvailableAmount();
		return Result.ok(AvailableAmount);
	}

	//Request URL: http://localhost:8500/api/account/userAccount/findUserConsumePage/1/10
	@GetMapping("/findUserConsumePage/{pn}/{pz}")
	@TingshuLogin
	@Operation(summary = "查询用户的消费记录")
	public Result findUserConsumePage(@PathVariable(value = "pn")Long pn ,
									  @PathVariable(value = "pz") Long pz ){

		Long userId = AuthContextHolder.getUserId();
		IPage<UserAccountDetail> page = new Page<>(pn,pz);
		page = userAccountService.findUserConsumePage(page,userId);
		return Result.ok(page);
	}

	//Request URL: http://localhost:8500/api/account/userAccount/findUserRechargePage/1/10
	@GetMapping("/findUserRechargePage/{pn}/{pz}")
	@TingshuLogin
	@Operation(summary = "查询用户的充值记录")
	public Result findUserRechargePage(@PathVariable(value = "pn")Long pn ,
									  @PathVariable(value = "pz") Long pz ){

		Long userId = AuthContextHolder.getUserId();
		IPage<UserAccountDetail> page = new Page<>(pn,pz);
		page = userAccountService.findUserRechargePage(page,userId);
		return Result.ok(page);
	}
}

